package com.example.administrator.newsapp;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.newsapp.pojo.Data;

import java.util.List;

/**
 * ClassName:
 * Description:
 */

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder>{
    private List<Data.ResultBean.DataBean> dataList;
    private Context mContext;
    public ListAdapter(Context mContext,List<Data.ResultBean.DataBean> dataList){
        this.dataList=dataList;
        this.mContext=mContext;
    }
    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView ivHead;
        TextView tvTitle;
        TextView tvAuthor;
        TextView tvTime;
        View mView;
        public ViewHolder(View itemView) {
            super(itemView);
            ivHead = itemView.findViewById(R.id.iv_head);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvAuthor=itemView.findViewById(R.id.tv_author);
            tvTime = itemView.findViewById(R.id.tv_time);
            mView = itemView;
        }
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item,viewGroup,false);
        final ViewHolder holder = new ViewHolder(view);
            holder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos  = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.mView,pos);
                }
            });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {
        Data.ResultBean.DataBean dataBean = dataList.get(i);
        viewHolder.tvTime.setText(dataBean.getDate());
        viewHolder.tvAuthor.setText(dataBean.getAuthor_name());
        viewHolder.tvTitle.setText(dataBean.getTitle());
        Glide.with(mContext).load(dataBean.getThumbnail_pic_s()).into(viewHolder.ivHead);

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setmOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
